package com.huikeportal.controller;

import java.util.Map;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.huike.model.MsgResponse;
import com.huike.tools.DateTool;
import com.huike.tools.MessageTool;
import com.huikeportal.model.ChainMenuInfo;
import com.huikeportal.model.common.InterFaceModel;
import com.huikeportal.service.ChainMenuService;
import com.huikeportal.service.InterFaceCommon;

@Controller
public class ChainMenuAPI {

	@Autowired
	private InterFaceCommon interFaceCommon;

	@Autowired
	private ChainMenuService chainMenuService;

	@RequestMapping(value = "/huike_getChainMenuList", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String getChainMenuList(String firstAddress, String secondAddress,
			String belongString, String roleId) {
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_getChainMenuList");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		// 校验参数
		if (roleId == null || "".equals(roleId) || belongString == null
				|| "".equals(belongString)) {
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		// 处理
		try {
			ChainMenuInfo chainMenuInfo = new ChainMenuInfo();
			if (firstAddress != null && !"".equals(firstAddress)) {
				chainMenuInfo.setParent1Id(firstAddress);
			}
			if (secondAddress != null && !"".equals(secondAddress)) {
				chainMenuInfo.setParent2Id(secondAddress);
			}
			chainMenuInfo.setBelongString(belongString);
			chainMenuInfo.setRoleId(roleId);
			Map<String, Object> map = chainMenuService
					.getChainMenuList(chainMenuInfo);
			msgResponse.setObject(map);
			msgResponse.setRespCode("90001");
			msgResponse.setRespCodeDes(MessageTool.RESP_90001);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("success");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		} catch (Exception e) {
			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
	}
}
